﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LxCFD
{
    public class AMGInContact : AMGBase,  ISolvableContact
    {
        public List<MGContact> MGContacts { get; set; } = new List<MGContact>();
        public BiCGStabInZone O_BiCGSolver;
        public BiCGStabInZone N_BiCGSolver;
        public void Create(IProvideContactTopology startContact, MGMeshCreator commonData, AMGInZone oZoneAMG, AMGInZone nZoneAMG, int oThreadID, int nThreadID)
        {
            //网格
            MGContacts.Clear();
            MGContactCreator contactCreator = commonData.contactCreator;
            MGContact con = contactCreator.CreateMGContact(startContact,
                 oZoneAMG.MgZones[0], nZoneAMG.MgZones[0],
                 oZoneAMG.MgZones[0].ThreadIndexs[oThreadID],
                 nZoneAMG.MgZones[0].ThreadIndexs[nThreadID]);
            MGContacts.Add(con);
            for (int i1 = 1; i1 < commonData.maxCoarseLevelLimit; i1++)
            {
                MGContact c = contactCreator.CreateMGContact(MGContacts[i1 - 1],
                oZoneAMG.MgZones[i1], nZoneAMG.MgZones[i1],
                oZoneAMG.MgZones[i1].ThreadIndexs[oThreadID],
                nZoneAMG.MgZones[i1].ThreadIndexs[nThreadID]);
                MGContacts.Add(c);
            }
            //设置数组
            MGContacts[0].SetArray(this);//contact);
            for (int i1 = 1; i1 < MGContacts.Count; i1++)
            {
                MGContacts[i1].SetArray(this);// contact);
                MGContacts[i1].SetFineContact(MGContacts[i1 - 1]);
            }
        }
        public void LinkToMatrix(IAMGSolvableContact connect)
        {
            MGContacts[0].SetFineContact(connect);
            isNeededUpdateA = true;
        }
        public void Restrict(int taskIndex)
        {
            MGContacts[SolvingLevel].Restrict_ALU(taskIndex);
        }
        public void Copy_xArray(int taskIndex)
        {
            MGContacts[SolvingLevel].Copy_xArray(taskIndex);
        }
        public void Copy_xCommonArray(int taskIndex)
        {
            MGContacts[SolvingLevel].Copy_xCommonArray(taskIndex);
        }
        public void Copy_yArray(int taskIndex)
        {
            MGContacts[SolvingLevel].Copy_yArray(taskIndex);
        }
        public void Copy_zArray(int taskIndex)
        {
            MGContacts[SolvingLevel].Copy_zArray(taskIndex);
        }
    }
}
//Console.WriteLine($"MGContact level {i1} facecount {MGContacts[i1].Pairs .Length}");
//public void AddBiCGSupport(BiCGStabSolverInContact solver)
//{
//    this.BiCGSolver = solver;
//}
//public void IniAndRestrict(int coarseLevel, int taskIndex)
//{
//    if (isNeededUpdateA)
//    {
//        MGContacts[coarseLevel].Restrict_Are(taskIndex);
//    }
//    else
//    {
//        MGContacts[coarseLevel].Restrict_re(taskIndex);
//    }
//}
//public void Prolong(int coarseLevel, int taskIndex)
//{
//    MGContacts[coarseLevel].Prolong(taskIndex);
//}
//public void IniAndRestrict_FCycleHead(int coarseLevel, int taskIndex)
//{
//    if (isNeededUpdateA)
//    {
//        MGContacts[coarseLevel].Restrict_Ar_FCycleHead(taskIndex);
//    }
//    else
//    {
//        MGContacts[coarseLevel].Restrict_r_FCycleHead(taskIndex);
//    }
//}
//public void Solve_Step1(int coarseLevel, int taskIndex)
//{
//    coarseContactList[coarseLevel].Cal_e_Jacobi_Step1(taskIndex);
//}
//public void Solve_Step2(int coarseLevel, int taskIndex)
//{
//    coarseContactList[coarseLevel].Cal_e_Jacobi_Step2(taskIndex);
//}
//public void Restrict(int coarseLevel, int taskIndex)
//{
//    coarseContactList[coarseLevel].Initialize_ArLUe(taskIndex);
//}